﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using System.Xml;
using System.Net;
using CarExam.Common;

namespace CarExam.Common
{
    public class HttpRequestUlity
    {
        /// <summary>
        /// 通过请求获取数据
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public DataTable GetDataFromRequest(string url)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            HttpWebResponse reponse = (HttpWebResponse)request.GetResponse();
            Stream s = reponse.GetResponseStream();
            byte[] rsByte = new byte[reponse.ContentLength];  //把流文件转换为字节数组
            try
            {
                s.Read(rsByte, 0, (int)reponse.ContentLength);
                string str = System.Text.Encoding.UTF8.GetString(rsByte, 0, rsByte.Length).ToString();
                DataTable dt = ConvertXMLToDataSet(str);
                if (dt != null && dt.Rows.Count > 0)
                {
                    return dt;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                CarExam.Common.ClassLoger.logger("error", ex.Message);
                return null;
            }
        }

        /// <summary>
        /// Xml转换为Table
        /// </summary>
        /// <param name="xml"></param>
        /// <returns></returns>
        private DataTable ConvertXMLToDataSet(string xml)
        {
            StringReader sr = null;
            XmlTextReader xtr = null;
            try
            {
                DataSet ds = new DataSet();
                sr = new StringReader(xml);
                xtr = new XmlTextReader(sr);
                ds.ReadXml(xtr);
                if (ds.Tables.Count > 0 && ds != null)
                {
                    return ds.Tables[0];
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (xtr != null)
                    xtr.Close();
            }
            return null;
        }

        /// <summary>
        /// GET请求获取信息
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string GetResponseInfo(string url)
        {
            string ret = string.Empty;
            HttpWebRequest web = null;
            try
            {
                web = (HttpWebRequest)HttpWebRequest.Create(url);
                web.Method = "GET";
                web.KeepAlive = false;
                web.Timeout = 2000 * 5;
                HttpWebResponse res = (HttpWebResponse)web.GetResponse();
                Stream s = res.GetResponseStream();
                StreamReader sr = new StreamReader(s, Encoding.UTF8);
                ret = sr.ReadLine();
                sr.Close();
                res.Close();
                s.Close();
            }
            catch (Exception ex)
            {
                ClassLoger.logger("Common/HtpRequestUlity/GetResponseInfo", ex.Message);
            }
            finally
            {
                if (web != null)
                    web.Abort();
            }
            return ret;
        }

        /// <summary>
        /// POST请求获取信息
        /// </summary>
        /// <param name="url"></param>
        /// <param name="paramData"></param>
        /// <returns></returns>
        public static string GetResponseInfo(string url, string paramData)
        {
            string ret = string.Empty;
            try
            {
                byte[] byteArray = System.Text.Encoding.Default.GetBytes(paramData); //转化
                HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(url));
                webReq.Method = "POST";
                webReq.ContentType = "application/x-www-form-urlencoded";

                webReq.ContentLength = byteArray.Length;
                Stream newStream = webReq.GetRequestStream();
                newStream.Write(byteArray, 0, byteArray.Length);//写入参数
                newStream.Close();
                HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
                StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                ret = sr.ReadToEnd();
                sr.Close();
                response.Close();
                newStream.Close();
            }
            catch (Exception ex)
            {
            }
            return ret;
        }
    }
}
